package com.zsrt.devbase.common.aspect.service;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;

import com.zsrt.devbase.common.annotation.DataSource;
import com.zsrt.devbase.common.datesource.DataSourceContextHolder;

@Aspect
@Order(0)
public class DataSourceAspect {

	public DataSourceAspect() {
		System.err.println("创建DataSourceAspect切面");
	}

	@After("execution(* com.zsrt.devbase.service.*.*(..))")
	public void after() throws Throwable {
		DataSourceContextHolder.clearDataSourceType();
	}

	@Before("execution(* com.zsrt.devbase.service.*.*(..)) && @annotation(com.zsrt.devbase.common.annotation.DataSource)")
	public void before(JoinPoint joinPoint) throws Throwable {
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();
		DataSource dataSource = method.getAnnotation(DataSource.class);
		String name = dataSource.name();
		DataSourceContextHolder.setDataSourceType(name);
	}

}
